﻿using System.Linq;
using System.Windows.Forms;
using AmiEmuLib.Host;
using AmiEmuLib.Native;

namespace amiemu_lib_test
{
    public partial class ProgressForm : HostWindow
    {
        public ProgressForm()
        {
            InitializeComponent();
        }

        public void AddLine(string a_line)
        {
            if (richTextBox.Lines.Length > 0)
                richTextBox.AppendText(System.Environment.NewLine);
            richTextBox.AppendText(a_line);

            richTextBox.SelectionStart = richTextBox.TextLength;
            richTextBox.ScrollToCaret();
        }

        public void UpdateLastLine(string a_line)
        {
            if (richTextBox.Lines.Length > 0)
                DeleteLine(richTextBox.Lines.Count() - 1);

            AddLine(a_line);
        }

        private void DeleteLine(int a_line)
        {
            int start_index = richTextBox.GetFirstCharIndexFromLine(a_line);
            int count = richTextBox.Lines[a_line].Length;

            // Eat new line chars
            if (a_line < richTextBox.Lines.Length - 1)
            {
                count += richTextBox.GetFirstCharIndexFromLine(a_line + 1) -
                    ((start_index + count - 1) + 1);
            }

            richTextBox.Text = richTextBox.Text.Remove(start_index, count);
        }
    }
}